<template>
	<view class="content">
		<view class="preview" id="video-container"></view>
	</view>
</template>

<script module="ezuikit" lang="renderjs">
	var player = null;
	export default {
		data() {
			return {
				detail: {
					
				},
				id:''
			}
		},
		mounted() {
			console.log('mounted...');
		
			if (typeof window.EZUIKit !== 'undefined') {
				
				console.log('defined EZUIKit...');
				this.initPlayer();
			} else {
				
				console.log('undefined EZUIKit...');				
				// 动态引入较大类库避免影响页面展示
				const script = document.createElement('script')
				// view 层的页面运行在 www 根目录，其相对路径相对于 www 计算
				script.src = 'static/ezuikit.js'
				script.onload = this.initPlayer.bind(this)
				document.head.appendChild(script)
			}
		},
		onLoad(option) {
			this.id = option.id
		},
		beforeDestroy(){
			this.stop()
		},
		methods: {
			initPlayer() {
				const {
					windowWidth,
					windowHeight
				} = uni.getSystemInfoSync();
				console.log('initPlayer...');
				var _this = this
				uni.request({
					url: 'http://www.yndzfl.cn/wxsite/User/getcamerainfo',
					method: 'POST',
					header:{
						"Content-Type" : "application/x-www-form-urlencoded"
					},
					data: {
						openid: uni.getStorageSync('OPENID'),
						id: _this.id
					},
					success(res) {
						console.log(res)
						if(res.data.code == 1){
							// _this.detail = res.data.data
							var accessToken = res.data.data.token;
							player = new EZUIKit.EZUIKitPlayer({
							  id: 'video-container', // 视频容器ID
							  accessToken: accessToken,
							  url: res.data.data.url,
							  // simple - 极简版; pcLive-pc直播；pcRec-pc回放；mobileLive-移动端直播；mobileRec-移动端回放;security - 安防版;voice-语音版;
							  template: 'security',
							  // plugin: ['talk'], // 加载插件，talk-对讲
							  width: windowWidth,
							  footer: ['fullScreen'],
							  height: windowWidth * 2 / 3,
							});
							window.player = player;
						}else if(res.data.code == -1){
							uni.showToast({
								title: res.data.msg,
								icon: 'none'
							})
						}else if(res.data.code == 408){
							uni.showToast({
								title: res.data.msg,
								icon: 'none'
							})
							uni.reLaunch({
								url: '/pages/admin/login'
							})
						}	
						
					}
				})	
				
			},
			play() {
				var playPromise = player.play();
				playPromise.then((data) => {
					console.log("promise 获取 数据", data)
				})
			},
			stop() {
				var stopPromise = player.stop();
				stopPromise.then((data) => {
					console.log("promise 获取 数据", data)
				})
			},
			getOSDTime() {
				var getOSDTimePromise = player.getOSDTime();
				getOSDTimePromise.then((data) => {
					console.log("promise 获取 数据", data)
				})
			},
			capturePicture() {
				var capturePicturePromise = player.capturePicture(`${new Date().getTime()}`);
				capturePicturePromise.then((data) => {
					console.log("promise 获取 数据", data)
				})
			},
			openSound() {
				var openSoundPromise = player.openSound();
				openSoundPromise.then((data) => {
					console.log("promise 获取 数据", data)
				})
			},
			closeSound() {
				var openSoundPromise = player.closeSound();
				openSoundPromise.then((data) => {
					console.log("promise 获取 数据", data)
				})
			},
			startSave() {
				var startSavePromise = player.startSave(`${new Date().getTime()}`);
				startSavePromise.then((data) => {
					console.log("promise 获取 数据", data)
				})
			},
			stopSave() {
				var stopSavePromise = player.stopSave();
				stopSavePromise.then((data) => {
					console.log("promise 获取 数据", data)
				})
			},
			ezopenStartTalk() {
				player.startTalk();
			},
			ezopenStopTalk() {
				player.stopTalk();
			},
			fullScreen() {
				player.fullScreen();
			},
			destroy() {
				var destroyPromise = player.destroy();
				destroyPromise.then((data) => {
					console.log("promise 获取 数据", data)
				})
			}
		}
	}
</script>

<style>

</style>
